package com.ai.module.mj.controller;

import cn.hutool.core.text.CharSequenceUtil;
import com.ai.module.chat.common.ano.NoAuth;
import com.ai.module.chat.config.advice.exception.BusinessException;
import com.ai.module.mj.domain.req.*;
import com.ai.module.mj.domain.vo.TaskChangeParams;
import com.ai.module.mj.service.ImageSubmitService;
import com.ai.module.user.service.ChatGptImageService;
import com.ai.module.mj.constant.enums.TaskAction;
import com.ai.module.mj.constant.stringConstant.ReturnCode;
import com.ai.module.mj.domain.vo.SubmitResultVO;
import com.ai.module.mj.util.ConvertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
/**
 * @author: 何晋庆
 * @create: 2023-12-10 12:55
 * @description: 绘画相关的 接口,对应页面的 /image ,/belong
 **/
@Api(tags = "任务提交")
@Slf4j
@RestController
@RequestMapping("/member/submit")
@RequiredArgsConstructor
public class SubmitController {

	@Autowired
	private ImageSubmitService centerService;

	@Autowired
	private ChatGptImageService  chatImageService;


	@ApiOperation(value = "提交Imagine任务")
	@PostMapping("/imagine")
	public SubmitResultVO imagine(@RequestBody SubmitImagineReq imagineDTO) {
		String prompt = imagineDTO.getPrompt();
		if (CharSequenceUtil.isBlank(prompt)) {
			throw new BusinessException(ReturnCode.VALIDATION_ERROR, "prompt不能为空");
		}
		String realPrompt = prompt.replaceAll("—", "-")
				.replaceAll("：", ":");
		imagineDTO.setPrompt(realPrompt);
		return centerService.imagine(imagineDTO);
	}

	@ApiOperation(value = "绘图变化-simple")
	@PostMapping("/simpleChange")
	public SubmitResultVO simpleChange(@RequestBody SubmitSimpleChangeReq simpleChangeDTO) {
		TaskChangeParams changeParams = ConvertUtils.convertChangeParams(simpleChangeDTO.getContent());
		if (changeParams == null) {
			throw new BusinessException(ReturnCode.VALIDATION_ERROR, "content参数错误");
		}
		return centerService.simpleChange(simpleChangeDTO);
	}

	@ApiOperation(value = "绘图变化")
	@PostMapping("/change")
	public SubmitResultVO change(@RequestBody SubmitChangeReq changeDTO) {
		if (CharSequenceUtil.isBlank(changeDTO.getTaskId())) {
			throw new BusinessException(ReturnCode.VALIDATION_ERROR, "taskId不能为空");
		}
		if (!Set.of(TaskAction.UPSCALE, TaskAction.VARIATION, TaskAction.REROLL).contains(changeDTO.getAction())) {
			throw new BusinessException(ReturnCode.VALIDATION_ERROR, "action参数错误");
		}
		return centerService.change(changeDTO);
	}

	@ApiOperation(value = "提交Describe任务")
	@PostMapping("/describe")
	public SubmitResultVO describe(@RequestBody SubmitDescribeReq describeDTO) {

		return centerService.describe(describeDTO);
	}

	@ApiOperation(value = "拼图功能")
	@PostMapping("/blend")
	public SubmitResultVO blend(@RequestBody SubmitBlendReq blendDTO) {
		List<String> base64Array = blendDTO.getBase64Array();
		if (base64Array == null || base64Array.size() < 2 || base64Array.size() > 5) {
			throw new BusinessException(ReturnCode.VALIDATION_ERROR, "base64List参数错误");
		}
		return centerService.blend(blendDTO);
	}

	@NoAuth
	@RequestMapping("notifyHook")
	public void notifyHook(String taskId){
		log.info("接收到回调请求[{}]",taskId);
	}
}
